package edu.ust.seis610.chart;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import edu.ust.seis610.GeneticPRG;

public class DynamicChart extends ApplicationFrame {

    /** The time series data. */
    private TimeSeries series;
    public String d;
    public JTextArea text = new JTextArea ();

    /** The most recent value added. */
    private double lastValue =90000000000.0;

    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public DynamicChart(final String title) {

        super(title);
        this.series = new TimeSeries("Avg Generational Fitness", Millisecond.class);
        final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
        final JFreeChart chart = createChart(dataset);

        final ChartPanel chartPanel = new ChartPanel(chart);
  
        text.setEditable (false);
        text.setLineWrap( true ); 
        text.setWrapStyleWord( true );
        text.setSize(100, 1);
        final JPanel content = new JPanel(new BorderLayout());
        content.add(chartPanel);
        content.add(text, BorderLayout.SOUTH);
        chartPanel.setPreferredSize(new java.awt.Dimension(1000, 540));
        setContentPane(content);
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return A sample chart.
     */
    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
            "Survival of the Function",
            "Time",
            "Avg Generational Fitness",
            dataset,
            true,
            true,
            false
        );
        final XYPlot plot = result.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis.setFixedAutoRange(60000.0);  // 60 seconds
        axis = plot.getRangeAxis();
        axis.setRange(0.0, 90000000000.0);
        return result;
    }

    public void graph1(double genAvg, String smallFunc, double smv, String genCnt ) {
    	
            this.lastValue = genAvg;
            final Millisecond now = new Millisecond();
            text.setText("Generation: "+ genCnt + "\nFunction with best fitness is F(x)=" + smallFunc + "\nFitness:" + smv);
            this.series.addOrUpdate(new Millisecond(), this.lastValue);
    }
    
    public void graph1(double genAvg, String smallFunc, double smv, String genCnt, String eTime ) {
    	
        this.lastValue = genAvg;
        final Millisecond now = new Millisecond();
        text.setText("Generation: "+ genCnt + "\nFunction with best fitness is F(x)=" + smallFunc + "\nFitness:" + smv +"\n"+eTime);
        this.series.addOrUpdate(new Millisecond(), this.lastValue);
}
}
